// api.js
let baseUrl = 'https://www.ruyiqifu.com.cn/api'; // 替换为你的API域名
// let baseUrl = 'http://192.168.0.12:8888'
 
function request(options) {
  return new Promise((resolve, reject) => {
    let token = uni.getStorageSync('userToken'); // 假设使用了token进行身份验证
    // let url = baseUrl + options.url
    let url = baseUrl  + options.url
    let data = options.data || {}
    let method = options.method
    let header = {}
    let loading = true
    if (loading) {
      uni.showLoading({
        title: 'loading'
      })
    }
    if (token) {
      header = {
        // 'content-type': 'application/json',
        Authorization: `Bearer ${token}`,
      }
    }
    
    uni.request({
      url,
      data,
      method,
      header: header,
      success: (res) => {
        loading = false
        if (!loading) {
          uni.hideLoading()
        }
        if (res.data.code !== '200') {
          wx.showToast({
            title: res.data.message,
            icon: 'none',
          })
        } else {
          resolve(res.data)
        }
      },
      fail: (err) => {
        // console.log(err);
        
        loading = false
        if (!loading) {
          uni.hideLoading()
        }
        wx.showToast({
          title: err.data.message,
          icon: 'none',
        })
        resolve(err)
      }
    })
    
  });
}
 
export default request;
